import { createRouter, createWebHistory } from 'vue-router';
import { isAuthenticated, isRecruiter, isApplicant } from '../services/authService';

// 布局组件
import Layout from '../components/Layout.vue';

// 页面组件
import Home from '../views/Home.vue';
import Login from '../views/Login.vue';
import Register from '../views/Register.vue';
import JobPost from '../views/JobPost.vue';
import MyJobs from '../views/MyJobs.vue';
import JobList from '../views/JobList.vue';
import JobDetail from '../views/JobDetail.vue';
import JobApplication from '../views/JobApplication.vue';

// 路由守卫
const requireAuth = (to, from, next) => {
  if (!isAuthenticated()) {
    next('/login');
  } else {
    next();
  }
};

const requireRecruiter = (to, from, next) => {
  if (!isAuthenticated()) {
    next('/login');
  } else if (!isRecruiter()) {
    next('/');
  } else {
    next();
  }
};

const requireApplicant = (to, from, next) => {
  if (!isAuthenticated()) {
    next('/login');
  } else if (!isApplicant()) {
    next('/');
  } else {
    next();
  }
};

// 路由配置
const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/login',
    name: 'Login',
    component: Login,
    beforeEnter: (to, from, next) => {
      if (isAuthenticated()) {
        // 如果已登录，根据角色重定向
        if (isRecruiter()) {
          next('/my-jobs');
        } else if (isApplicant()) {
          next('/jobs');
        } else {
          next('/');
        }
      } else {
        next();
      }
    }
  },
  {
    path: '/register',
    name: 'Register',
    component: Register,
    beforeEnter: (to, from, next) => {
      if (isAuthenticated()) {
        // 如果已登录，根据角色重定向
        if (isRecruiter()) {
          next('/my-jobs');
        } else if (isApplicant()) {
          next('/jobs');
        } else {
          next('/');
        }
      } else {
        next();
      }
    }
  },
  {
    path: '/',
    component: Layout,
    children: [
      {
        path: '/post-job',
        name: 'JobPost',
        component: JobPost,
        beforeEnter: requireRecruiter
      },
      {
        path: '/post-job/:id',
        name: 'JobEdit',
        component: JobPost,
        beforeEnter: requireRecruiter,
        props: true
      },
      {
        path: '/my-jobs',
        name: 'MyJobs',
        component: MyJobs,
        beforeEnter: requireRecruiter
      },
      {
        path: '/jobs',
        name: 'JobList',
        component: JobList,
        beforeEnter: requireApplicant
      },
      {
        path: '/jobs/:id',
        name: 'JobDetail',
        component: JobDetail,
        beforeEnter: requireAuth,
        props: true
      },
      {
        path: '/jobs/:id/apply',
        name: 'JobApplication',
        component: JobApplication,
        beforeEnter: requireApplicant,
        props: true
      },
      {
        path: '/my-applications',
        name: 'MyApplications',
        component: () => import('../views/MyApplications.vue'),
        beforeEnter: requireApplicant
      }
    ]
  }
];

// 创建路由器实例
const router = createRouter({
  history: createWebHistory(),
  routes
});

// 全局前置守卫
router.beforeEach((to, from, next) => {
  // 初始化存储
  if (typeof localStorage !== 'undefined') {
    // 可以在这里添加存储初始化逻辑
  }
  
  next();
});

export default router;